package com.uchengwang.ui.cm_ui.umsg.http;

import android.app.Activity;
import android.text.TextUtils;
import android.util.Log;

import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.config.WantChatConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.HttpCallBack;
import com.uchengwang.functions.http.listener.OnHttpResultListener;
import com.uchengwang.functions.http.listener.OnHttpResultSingleListener;
import com.uchengwang.functions.http.listener.OnResultListener;
import com.uchengwang.functions.http.manager.HttpManager;
import com.uchengwang.ui.cm_ui.umsg.bean.WantChatBean;
import com.uchengwang.ui.cm_ui.umsg.bean.WantChatListBean;
import com.uchengwang.ui.cm_ui.umsg.bean.WantChooseImgBean;
import com.uchengwang.ui.utils.JsonUtils;
import com.uchengwang.ui.utils.LoadingUtils;

import java.util.List;

/**
 * Created by U城网 on 2017/12/14.
 * 享聊的网络请求
 */

public class ChatHttpManager {

    private static ChatHttpManager manager;
    private ChatHttpManager(){}

    public static ChatHttpManager getManager() {
        if (manager == null){
            manager = new ChatHttpManager();
        }
        return manager;
    }

    /**
     *获取享聊列表
     * @param wantType 享聊的类型 目前只需填1
     * @param sex 0=女 1=男 -1获取全部
     * @param uniId 获取自己学校的  不传获取全部的
     * */
    public void getChatDatas(Activity activity, int wantType, int page, int sex, long uniId, final OnHttpResultListener<WantChatBean> listener){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.CHAT_TYPE, wantType);
        map.put(BaseConfig.PAGE, page);
        map.put(BaseConfig.PERPAGE, 10);
        if (sex >=0){
            map.put(BaseConfig.WANT_SEX, sex);
        }

        if (uniId > 0){
            map.put(BaseConfig.UNI_ID, uniId);
        }
        HttpManager.newInstance(activity).query(WantChatConfig.Chat.method_get, WantChatConfig.Chat.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                listener.onResult(status, msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                if (status ==0 && !TextUtils.isEmpty(data) && !data.equals("[]")){
                    WantChatListBean bean = JsonUtils.getManager().stringToBean(data, WantChatListBean.class);
                    listener.onResult(status, msg, bean.getTotalPage(), bean.getData());
                }else {
                    listener.onResult(status, msg, 0, null);
                }
            }
        });
    }

    /**
     * 获取某一个人的享聊信息
     * */
    public void getChatOne(Activity activity, int chatType, long uid, final OnHttpResultSingleListener<WantChatBean> listener){
        LoadingUtils.getUtils().loading(activity);
        MapRequest map = new MapRequest();
        map.put(BaseConfig.CHAT_TYPE, chatType);
        HttpManager.newInstance(activity).queryCheck(WantChatConfig.GetChatOne.method, WantChatConfig.GetChatOne.URL+"/"+uid, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                LoadingUtils.getUtils().hide();
                listener.onResult(status, msg, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                LoadingUtils.getUtils().hide();
                if (status ==0 && !TextUtils.isEmpty(data)){
                    WantChatBean bean = JsonUtils.getManager().stringToBean(data, WantChatBean.class);
                    listener.onResult(status, msg, bean);
                }else {
                    listener.onResult(status, msg, null);
                }
            }
        });
    }

    /**
     * 上传享聊资料
     * */
    public void postWantInfo(Activity activity, MapRequest map, final OnResultListener listener){
        LoadingUtils.getUtils().loading(activity);
        HttpManager.newInstance(activity).query(WantChatConfig.Chat.method_post, WantChatConfig.Chat.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                LoadingUtils.getUtils().hide();
                listener.onResult(status, msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                LoadingUtils.getUtils().hide();
                listener.onResult(status, msg, 0, null);
            }
        });

    }



    /**
     * 获取自己的享聊数据
     * @param type 享聊类型
     * */
    public void getMyChatInfo(Activity activity, int type, final OnHttpResultSingleListener<WantChatBean> listener){
        LoadingUtils.getUtils().loading(activity);
        MapRequest map = new MapRequest();
        map.put(BaseConfig.CHAT_TYPE, type);
        HttpManager.newInstance(activity).queryCheck(WantChatConfig.ChatMy.method, WantChatConfig.ChatMy.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                LoadingUtils.getUtils().hide();
                listener.onResult(status, msg, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                LoadingUtils.getUtils().hide();
                if (status ==0 && !TextUtils.isEmpty(data)){
                    WantChatBean bean = JsonUtils.getManager().stringToBean(data, WantChatBean.class);
                    listener.onResult(status, msg, bean);
                }else {
                    listener.onResult(status, msg, null);
                }
            }
        });
    }

    /**
     * 修改自己的享聊信息
     * */
    public void changeWantInfo(Activity activity, MapRequest map, final OnResultListener listener){
        LoadingUtils.getUtils().loading(activity);
        HttpManager.newInstance(activity).query(WantChatConfig.Chat.method_put, WantChatConfig.Chat.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                LoadingUtils.getUtils().hide();
                listener.onResult(status, msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                LoadingUtils.getUtils().hide();
                listener.onResult(status, msg, 0, null);
            }
        });
    }


    /**
     * 创建一条享聊记录
     * */
    public void createChat(Activity activity, int chatType, long uid, final OnResultListener listener){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.CHAT_TYPE, chatType);
        map.put(BaseConfig.UID, uid);
        HttpManager.newInstance(activity).query(WantChatConfig.ChatHistory.method, WantChatConfig.ChatHistory.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                if (listener != null){
                    listener.onResult(status, msg, 0, null);
                }
            }

            @Override
            public void onResp(int status, String msg, String data) {
                if (listener != null){
                    listener.onResult(status, msg, 0, data);
                }
            }
        });
    }


    /**
     * 获取享聊审核数据
     * @param chatType 享聊类型
     * @param examine 审核状态 -1=未通过 0=待审核 1=已通过
     * */
    public void getWantExamine(Activity activity, int chatType, int examine, int page, final OnHttpResultListener<WantChatBean> listener){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.PAGE, page);
        map.put(BaseConfig.PERPAGE, 10);
        map.put(BaseConfig.CHAT_TYPE, chatType);
        map.put(BaseConfig.IS_EXAMINE, examine);
        HttpManager.newInstance(activity).query(WantChatConfig.ExamineChat.method_get, WantChatConfig.ExamineChat.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                listener.onResult(status, msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                if (status==0 && !TextUtils.isEmpty(data)){
                    WantChatListBean bean = JsonUtils.getManager().stringToBean(data, WantChatListBean.class);
                    listener.onResult(status, msg, bean.getTotalPage(), bean.getData());
                }else {
                    listener.onResult(status, msg, 0, null);
                }
            }
        });
    }

    /**
     * 审核一条享聊信息
     * @param uid 被审核人的ID
     * @param isPass true通过
     * @param examineMsg 未通过信息
     * @param chatType 享聊类型
     * */
    public void wantExamine(Activity activity, int chatType, long uid, boolean isPass, String examineMsg, final OnResultListener listener){
        LoadingUtils.getUtils().loading(activity);
        MapRequest map = new MapRequest();
        map.put(BaseConfig.UID, uid);
        if (!TextUtils.isEmpty(examineMsg)){
            map.put(BaseConfig.EXAMINE_MSG, examineMsg);
        }
        map.put(BaseConfig.IS_PASS, isPass);
        map.put(BaseConfig.CHAT_TYPE, chatType);
        HttpManager.newInstance(activity).query(WantChatConfig.ExamineChat.method_put, WantChatConfig.ExamineChat.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                LoadingUtils.getUtils().hide();
                listener.onResult(status, msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                LoadingUtils.getUtils().hide();
                listener.onResult(status, msg, 0, null);
            }
        });

    }

    /**
     * 发送一个小灰条提醒信息
     * */
    public void sendMsg(Activity activity, String msg, String toUid, final OnResultListener listener){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.MSG, msg);
        map.put(BaseConfig.TO_UID, toUid);
        HttpManager.newInstance(activity).query(WantChatConfig.sendMsg.method, WantChatConfig.sendMsg.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("jjf", "小灰条发送失败");
                if (listener != null){
                    listener.onResult(status, msg, 0, null);
                }
            }

            @Override
            public void onResp(int status, String msg, String data) {
                if (status ==0){
                    Log.d("jjf", "小灰条发送成功");
                }else {
                    Log.d("jjf", "小灰条发送失败");
                }
                if (listener != null){
                    listener.onResult(status, msg, 0, null);
                }
            }
        });
    }

    /**
     * 举报某人
     * */
    public void report(Activity activity, String reportReason, long reportUid, final OnResultListener listener){
        LoadingUtils.getUtils().loading(activity);
        MapRequest map = new MapRequest();
        map.put(BaseConfig.REPORT_UID, reportUid);
        map.put(BaseConfig.REPORT_REASON, reportReason);
        HttpManager.newInstance(activity).query(WantChatConfig.ChatReport.method, WantChatConfig.ChatReport.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                LoadingUtils.getUtils().hide();
                listener.onResult(status, msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                LoadingUtils.getUtils().hide();
                listener.onResult(status, msg, 0, null);
            }
        });
    }


    /**
     * 这个不是网络的方法,  修改享聊的本地方法
     * */
    public String getHeadString(List<WantChooseImgBean> list){
        if (list != null && list.size() > 0){
            StringBuilder builder = new StringBuilder();
            for (int i=0; i<list.size(); i++){
                String ossUrl = list.get(i).getFileUrl();
                if (i != 0){
                    builder.append(";");
                }
                builder.append(ossUrl);
            }
            return builder.toString();
        }

        return null;

    }

}
